package com.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;

public class GameUtils {
	private Logger logger = Logger.getLogger(this.getClass());
 

	public static int strLength(String s) {
		int length = 0;
		int totalLength = s.trim().getBytes().length;
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(s);
		int singleChineseLen = 0;
		while (m.find()) {
			if (singleChineseLen == 0)
				singleChineseLen = m.group(0).getBytes().length;
			length += m.group(0).getBytes().length;
		}
		int englishLength = totalLength - length;

		int finalChineseLength = 0;
		if (singleChineseLen != 0)
			finalChineseLength = length / singleChineseLen * 2;

		length = englishLength + finalChineseLength;

		return length;
	}
    public static IoBuffer Serialize(IoBuffer buf){
    	IoBuffer buffer = IoBuffer.allocate(buf.remaining());
    	buffer.setAutoExpand(true);
    	buffer.put(buf);
    	buf.rewind();
    	buffer.flip();
		return buffer;
    }
 

}
